Learn R Programming

VeryLargeIntegers (version 0.1.8)

01. Basics: Very Large Integers Basics

Description

vli is a S3 class that allows to store and operate with arbitrarily large integers. Each object of class vli has 3 attributes (sign, length and value) that can be accessed as shown in the examples. The (absolute) value of the number is stored in a numeric vector to avoid truncation.

Usage

as.vli(n)

# S3 method for default as.vli(n)

# S3 method for character as.vli(n)

# S3 method for numeric as.vli(n)

asnumeric(x)

# S3 method for default asnumeric(x)

# S3 method for vli asnumeric(x)

# S3 method for vli as.integer(x, ...)

# S3 method for vli as.integer(x, ...)

vli(m)

# S3 method for vli print(x, ...)

is.vli(x)

Arguments

n

value for the vli object being created; character or numeric

x

object of class vli

...

further arguments passed to or from other methods

m

number of vli objects being initialized; numeric

Author

Javier Leiva Cuadrado

Details

In as.vli(n), if n is numeric, it must be a 32 bits integer to avoid the loss of precision. The idea is to use numeric objects only for small numbers. In other case, character objects are prefered. The function as.integer(x), where x a vli object, only works when the absolute value of x is up to 2.147.483.648 (32 bits). In other case it returns an error. The function asnumeric(x) could cause loss of precision if the value of x is big. The function vli(m) initialize a list of m objects of class vli. Punctuation signs are ignored in the creation of vli objects (see the last example).

Examples

Run this code
## Creating a new vli object
x <- as.vli("-89027148538375418689123052")

## Printing a vli object
print(x)

## Testing the class
is.vli(x)

## Coercing into a character object
as.character(x)

## Accessing to the attributes of the vli object
x$sign
x$value
x$length

## Punctuation signs are ignored
as.vli("2345.25")

Run the code above in your browser using DataLab